#!/usr/bin/python3
import os
import joblib
import sys
import matplotlib.pyplot
sys.path.append(os.path.abspath("../tools/"))
from feature_format import featureFormat, targetFeatureSplit
from sklearn import linear_model

DATA_DICT_PATH = "../final_project/final_project_dataset.pkl"

def identify_by_bonus(bonus):
    for person in data_dict:
        if data_dict[person]["bonus"] == bonus:
            return person

def draw_scatterplot(data):
    matplotlib.pyplot.xlabel("salary")
    matplotlib.pyplot.ylabel("bonus")
    highest_bonus = 0
    for point in data:
        salary = point[0]
        bonus = point[1]
        if bonus > highest_bonus:
            highest_bonus = bonus
        matplotlib.pyplot.scatter(salary, bonus)
    matplotlib.pyplot.show()
    return highest_bonus

def run_original():
    ### read in data dictionary, convert to numpy array
    data_dict = joblib.load( open(DATA_DICT_PATH, "rb") )
    features  = ["salary", "bonus"]
    formatted = featureFormat(data_dict, features)
    hb = draw_scatterplot(formatted)
    print(identify_by_bonus(hb))

# saw outliers in the scatterplot
# tehre are some stray plots, 
# but 4 of them represent highly paid individuals
def get_highest_bonus(dataset, how_many=4):
    sorted_dataset = dict(sorted(dataset.items(),
        reverse=True,
        key=lambda item: item[1]["bonus"] \
            if isinstance(item[1]["bonus"], int) \
            else 0))
    i = 0
    for person in sorted_dataset:
        print(person, 
              sorted_dataset[person]["bonus"],
              sorted_dataset[person]["salary"])
        # print(sorted_dataset[person]["bonus"])
        i += 1
        if i == how_many:
            break
    pass

### your code below
if __name__ == "__main__":
    data_dict = joblib.load( open(DATA_DICT_PATH, "rb") )
    data_dict.pop("TOTAL")
    features  = ["salary", "bonus"]
    formatted = featureFormat(data_dict, features)
    hb = draw_scatterplot(formatted)

    get_highest_bonus(data_dict)

